/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import com.github.pagehelper.PageInfo;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.SiteUser;
import me.zhengjie.modules.system.service.DeptService;
import me.zhengjie.modules.system.service.SiteUserService;
import me.zhengjie.modules.system.service.dto.SiteUserDto;
import me.zhengjie.modules.system.service.dto.SiteUserQueryCriteria;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author HeHui
* @date 2021-12-20
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "站长信息管理")
@RequestMapping("/api/siteUser")
public class SiteUserController {

    private final SiteUserService siteUserService;

    private final DeptService deptService;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('siteUser:list')")
    public void download(HttpServletResponse response, SiteUserQueryCriteria criteria) throws IOException {
        siteUserService.download(siteUserService.queryAll(criteria), response);
    }

    @GetMapping
    //@Log("查询站长信息")
    @ApiOperation("查询站长信息")
    @PreAuthorize("@el.check('siteUser:list')")
    public ResponseEntity<Object> query(SiteUserQueryCriteria criteria, Pageable pageable){
        PageInfo<SiteUserDto> result = siteUserService.page(criteria, pageable);
        return new ResponseEntity<>(PageUtil.toPage(result.getList(), result.getTotal()),HttpStatus.OK);
    }

    /**
     * 详情
     *
     * @param id id
     * @return {@link ResponseEntity}<{@link SiteUserDto}>
     */
    @GetMapping("/details")
    public ResponseEntity<SiteUserDto> details(@RequestParam Long id) {
        return new ResponseEntity<>(siteUserService.findById(id),HttpStatus.OK);
    }

//    @PostMapping
//    @Log("新增站长信息")
//    @ApiOperation("新增站长信息")
//    @PreAuthorize("@el.check('siteUser:add')")
//    public ResponseEntity<Object> create(@Validated @RequestBody SiteUser resources){
//        return new ResponseEntity<>(siteUserService.create(resources),HttpStatus.CREATED);
//    }

    @PutMapping
    @Log("修改站长信息")
    @ApiOperation("修改站长信息")
    @PreAuthorize("@el.check('siteUser:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody SiteUser resources){
        resources.setDeptId(resources.getId());
        boolean exists = siteUserService.exists(resources.getId());
        final long userId = SecurityUtils.getCurrentUserId();
        Timestamp now = new Timestamp(System.currentTimeMillis());
        if (!exists) {
            resources.setCreateTime(now);
            resources.setLastUpdateTime(now);
            resources.setCreateUserId(userId);
            resources.setLastUpdateId(userId);
            resources.setIsDeleted(0);
            siteUserService.create(resources);
        } else {
            resources.setLastUpdateId(userId);
            resources.setLastUpdateTime(now);
            siteUserService.update(resources);
        }

        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除站长信息")
    @ApiOperation("删除站长信息")
    @PreAuthorize("@el.check('siteUser:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        siteUserService.deleteInDeptId(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}